<?php

namespace app\agent\controller;

use app\common\controller\Agent;
use app\common\model\Images;
use app\common\model\OrderAftersalesRecord;
use think\Db;
use think\Exception;

/**
 * 资金明细
 *
 * @icon fa fa-user
 */
class MoneyRecord extends Agent
{

    protected $relationSearch = true;
    protected $multiFields = 'status';

    protected $model = null;
    protected $agent_id = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('AgentMoneyRecord');
        $agent_info = session('agent');
        $this->agent_id = $agent_info['id'];
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $filter = $this->request->get("filter", '');
            $filter = (array)json_decode($filter, true);
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model->with(['order'])->where($where)->where(['agent_money_record.agent_id'=>$this->agent_id])->order($sort, $order)->paginate($limit)->each(function ($item){
                $item['shop_name'] = '';
                if (in_array($item['type'],[1,2])){
                    $item['shop_name'] = \app\common\model\Shop::where('id',$item['order']['shop_id'])->value('shop_name');
                }
                return $item;
            });
            if (empty($filter['type'])){ //全统计
                $ljcb = $this->model->with(['agent','order'])->where($where)->where('type',1)->order($sort, $order)->sum('agent_money_record.money');
                $ljfy = $this->model->with(['agent','order'])->where($where)->where('type',2)->order($sort, $order)->sum('agent_money_record.money');
                $ljtx = $this->model->with(['agent','order'])->where($where)->where('type',3)->order($sort, $order)->sum('agent_money_record.money');
                $ljth = $this->model->with(['agent','order'])->where($where)->where('type',4)->order($sort, $order)->sum('agent_money_record.money');
                $count = [['name'=>'累计派发佣金','value'=>$ljfy],['name'=>'累计返还成本','value'=>$ljcb],['name'=>'累计提现','value'=>$ljtx],['name'=>'累计退回','value'=>$ljth]];
            }else{ //按类别统计
                $lj_money = $this->model->with(['agent','order'])->where($where)->where('type',$filter['type'])->order($sort, $order)->sum('agent_money_record.money');
                $arr = ['','累计返还成本','累计派发佣金','累计提现','累计退回'];
                $name = $arr[$filter['type']];
                $count = [['name'=>$name,'value'=>$lj_money]];
            }
            $result = array("total" => $list->total(), "rows" => $list->items(),'extend'=>['count'=>$count]);
            return json($result);
        }
        return $this->view->fetch();
    }

    //订单详情
    public function detail()
    {
        $id = $this->request->param('ids');
        if (!$id) {
            $this->error('参数错误');
        }
        $row = $this->model
            ->where('agent_money_record.id', $id)
            ->with(['agent','order'])
            ->find();

        if (!$row) {
            $this->error('记录未找到');
        }
        $this->view->assign('row', $row->toArray());
        return $this->view->fetch();
    }



}
